/**
 * Created by xuweijie on 2017/5/19.
 */
// 模态框提交
$('.upmodel').click(function () {
    var consignee = $('#upconsignee').val();
    var address = $('#upaddress').val();
    var phone = $('#upphone').val();
    var postcode = $('#uppostcode').val();
    var country = $('.country option:selected').val();
    var provience = $('.provience option:selected').val();
    var city = $('.city option:selected').val();
    var numprice = $('.numprice').val();
    var purchaseid = $('.purchaseid').val();
    if (consignee !== "" && address !== "" && phone !== "" && postcode !== "") {
        var params = $.param({
            consignee: consignee,
            address: address,
            phone: phone,
            postcode: postcode,
            country: country,
            provience: provience,
            city: city,
            flag: '0'
        }, true);
        $.ajax({
            url: '/insertaddr.action',
            type: 'post',
            data: params,
            dateType: 'json',
            success: function (data) {
                if (data == "0") {
                    alert("新地址添加成功!", "", function () {
                        window.location.href = '/front/submitorder/submitorderlist?numprice=' + numprice + '&purchaseid=' + purchaseid;
                    }, {type: 'success', confirmButtonText: '确定'});

                } else if (flag == "0" && data == "1") {
                    alert("新地址添加失败!", "", function () {
                    }, {type: 'error', confirmButtonText: '确定'});
                }
            }
        });
        $('#updateModal').modal('hide');
    } else {
        alert("必填内容不允许空值!", "", function () {
        }, {type: 'warning', confirmButtonText: '确定'});
    }
});


$(function () {
    /*选择优惠券的时候改变结算金额*/
    $('input[name=myqponvalue]').click(function () {
        var qpval = $(this).attr('qpval');
        $('#qpon').empty();
        $('#qpon').append(qpval);
        var totalp = $('#totalprice')[0].innerHTML;
        totalp = parseFloat(totalp) - parseFloat(qpval);
        $('#totalprice').empty();
        $('#totalprice')[0].append(totalp);
    });

    //提交订单
    $('.finalsubmitorder').click(function () {
        var purchaseid = $('.purchaseid').val();
        var addressid = $('.addressclass option:selected').val();
        var myqponid = $('input[name=myqponvalue]:checked').val();
        var paytype = $('input[name=paytype]:checked').val();
        var totalprice = $('#totalprice')[0].innerText;
        var exfee = $('#expfee')[0].innerText;
        var notes = $('#comment').val();
        if (paytype != null && addressid!=null) {
            var params = $.param({
                purchaseid: purchaseid,
                addressid: addressid,
                myqponid: myqponid,
                paytype: paytype,
                totalprice: totalprice,
                exfee: exfee,
                notes: notes
            }, true);
            $.ajax({
                url: '/payorder.action',
                type: 'post',
                data: params,
                dateType: 'json',
                success: function (data) {
                    if (data == "0") {
                        confirm("请问是否支付?", "", function (isConfirm) {
                            if (isConfirm) {
                                $.ajax({
                                    url: '/payI',
                                    type: "post",
                                    data: {
                                        purchaseId: purchaseid
                                    },
                                    dataType: 'json',
                                    success: function (data2) {
                                        var obj = eval('(' + data2 + ')');
                                        if (obj.status === 1) {
                                            alert(obj.msg, "", function () {
                                                window.location.href = '/front/pay/paylist';
                                            }, {type: 'success', confirmButtonText: '确定'});
                                        } else {
                                            alert(obj.msg, "", function () {
                                                window.location.href = '/front/purchase/waits';
                                            }, {type: 'error', confirmButtonText: '确定'});
                                        }
                                    }
                                });
                            } else {
                                window.location.href = '/front/purchase/waits';
                            }
                        }, {type: 'question', confirmButtonText: '确定', cancelButtonText: '取消'});
                    } else if (data == "1") {
                        alert("下单失败", "", function () {
                            window.location.href = "/front/mybuycar/buycarlist";
                        }, {type: 'error', confirmButtonText: '确定'});
                    }
                }
            });
        } else {
            alert("请完善订单信息", "", function () {
            }, {type: 'warning', confirmButtonText: '确定'});
        }
    });
});


